;; This file is part of scheme-GNUnet.
;; Copyright (C) 2021 Maxime Devos
;;
;; scheme-GNUnet is free software: you can redistribute it and/or modify it
;; under the terms of the GNU Affero General Public License as published
;; by the Free Software Foundation, either version 3 of the License,
;; or (at your option) any later version.
;;
;; scheme-GNUnet is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; Affero General Public License for more details.
;;
;; You should have received a copy of the GNU Affero General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;
;; SPDX-License-Identifier: AGPL3.0-or-later

(import (gnu gnunet utils bv-slice)
	(srfi srfi-26)
	(ice-9 match)
	(rnrs conditions)
	(rnrs bytevectors))

(test-begin "bv-slice")


;; slice-copy!

;; TODO maybe more specific conditions
(test-error "destination of slice-copy! must be writable"
  &assertion
  (slice-copy! (make-slice/read-write 9)
	       (slice/read-only (make-slice/read-write 9))))

(test-error "source of slice-copy! must be readable"
  &assertion
  (slice-copy! (slice/write-only (make-slice/read-write 9))
	       (make-slice/read-write 9)))

(test-error "lengths must match (1)"
  &assertion
  (slice-copy! (make-slice/read-write 9)
	       (make-slice/read-write 0)))

(test-error "lengths must match (2)"
  &assertion
  (slice-copy! (make-slice/read-write 0)
	       (make-slice/read-write 9)))

(test-equal "slice-copy! copies"
  #vu8(0 1 2 3)
  (let ((source (bv-slice/read-write #vu8(0 1 2 3)))
	(dest   (make-slice/read-write 4)))
    (slice-copy! source dest)
    (slice-bv dest)))

(test-equal "also if there's an offset in the source"
  #vu8(0 1 2 3)
  (let ((source (slice-slice (bv-slice/read-write #vu8(0 0 1 2 3)) 1))
	(dest   (make-slice/read-write 4)))
    (slice-copy! source dest)
    (slice-bv dest)))

(test-equal "also if the destination bv is long"
  #vu8(9 8 0 1 2 3)
  (let ((source (bv-slice/read-write #vu8(8 0 1 2)))
	(dest (slice-slice
	       (bv-slice/read-write (bytevector-copy #vu8(9 7 7 7 7 3)))
	       1 4)))
    (slice-copy! source dest)
    (slice-bv dest)))



(test-equal "slice-zero! writes zeros"
  #vu8(1 2 0 0 5 6 7 8)
  (let ((dest
	 (slice-slice
	  (bv-slice/read-write (bytevector-copy #vu8(1 2 3 4 5 6 7 8)))
	  2 2)))
    (slice-zero! dest)
    (slice-bv dest)))

(test-error "slice-zero! requires writability"
  &assertion
  (slice-zero! (slice/write-only (make-slice/read-write 9))))

(test-error "even if the length is zero"
  &assertion
  (slice-zero! (slice/write-only (make-slice/read-write 0))))

(define (some-numbers N)
  (map (cut expt 2 <>) (iota N)))
(define sizes/u `(#(16 ,slice-u16-ref ,slice-u16-set!)
		  #(32 ,slice-u32-ref ,slice-u32-set!)
		  #(64 ,slice-u64-ref ,slice-u64-set!)))
(define sizes/s `(#(16 ,slice-s16-ref ,slice-s16-set!)
		  #(32 ,slice-s32-ref ,slice-s32-set!)
		  #(64 ,slice-s64-ref ,slice-s64-set!)))

(for-each
 (match-lambda
   (#(bits ref set!)
    (test-equal
	(string-append "slice-u" (number->string bits) "-ref/set! round-trips")
      (some-numbers bits)
      (map (lambda (number)
	     ;; #xde: filler that should be unused
	     (define bv (make-bytevector (/ bits 8) #xde))
	     (define sl (bv-slice/read-write bv))
	     (set! sl 0 number (endianness little))
	     (ref sl 0 (endianness little)))
	   (some-numbers bits)))))
 sizes/u)

(for-each
 (match-lambda
   (#(bits ref set!)
    (test-equal
	(string-append "slice-s" (number->string bits) "-ref/set! round-trips")
      (append (map - (some-numbers bits))
	      ;; -1: avoid the sign bit
	      (some-numbers (- bits 1)))
      (map (lambda (number)
	     ;; #xde: filler that should be unused
	     (define bv (make-bytevector (/ bits 8) #xde))
	     (define sl (bv-slice/read-write bv))
	     (set! sl 0 number (endianness little))
	     (ref sl 0 (endianness little)))
	   (append (map - (some-numbers bits))
		   (some-numbers (- bits 1)))))))
 sizes/s)

;; Signed integer representations are used in some network messages,
;; so make sure they will be interpreted the same no matter the
;; architecture.
(test-equal "two's complement is used"
  -128
  (slice-s8-ref (bv-slice/read-write #vu8(#b10000000)) 0))

(test-end "bv-slice")

;; ^ TODO: test other procedures
